package com.lahuan.service.impl;

import com.lahuan.config.datasouce.CustomTransaction;
import com.lahuan.service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author lfy
 */
@Component
public class TestServiceImpl implements TestService {

    @Resource(name="jdbcTemplate1")
    JdbcTemplate jdbcTemplate1;
    @Resource(name="jdbcTemplate2")
    JdbcTemplate jdbcTemplate2;
    /**
     * 注入自己，通过spring调用才能使aop生效
     */
    @Autowired
    TestServiceImpl testService;


    @CustomTransaction(confirmMethod = "confirmMethod", cancelMethod = "cancelMethod")
    @Override
    public void testSuccess(int a,int b) {
        System.out.println("进入testSuccess(),参数:"+a+","+b);
        testService.tryA(a,b);
        testService.tryB(a,b);
    }

    @CustomTransaction(confirmMethod = "confirmMethod", cancelMethod = "cancelMethod")
    @Override
    public void testTryError(int a, int b) {
        System.out.println("进入testTryError(),参数:"+a+","+b);
        testService.tryA(a,b);
        testService.tryErrorB(a,b);
    }
    @CustomTransaction(confirmMethod = "confirmMethod", cancelMethod = "cancelMethod")
    @Override
    public void testConfirmError(int a, int b) {
        System.out.println("进入testConfirmError(),参数:"+a+","+b);
        testService.tryA(a,b);
        testService.trySuccessConfirmErrorB(a,b);
    }
    @Override
    public void confirmMethod(int a, int b) {
        System.out.println("进入confirmMethod(),参数:"+a+","+b);
    }

    @Override
    public void cancelMethod(int a, int b) {
        System.out.println("进入cancelMethod(),参数:"+a+","+b);
    }

    @CustomTransaction(confirmMethod = "confirmA", cancelMethod = "cancelA")
    @Override
    public void tryA(int a, int b) {
        jdbcTemplate1.execute("INSERT INTO `test_transaction`(`content`) VALUES ('tryA');");
    }

    @Override
    public void confirmA(int a, int b) {
        System.out.println("confirmA");
        jdbcTemplate1.execute("INSERT INTO `test_transaction`(`content`) VALUES ('confirmA');");
    }

    @Override
    public void cancelA(int a, int b) {
        System.out.println("cancelA");
        jdbcTemplate1.execute("INSERT INTO `test_transaction`(`content`) VALUES ('cancelA');");
    }




    @CustomTransaction(confirmMethod = "confirmB", cancelMethod = "cancelB")
    @Override
    public void tryB(int a, int b) {
        jdbcTemplate2.execute("INSERT INTO `test_transaction`(`content`) VALUES ('tryB');");
    }






    @Override
    public void confirmB(int a, int b) {
        System.out.println("confirmB");
        jdbcTemplate2.execute("INSERT INTO `test_transaction`(`content`) VALUES ('confirmB');");
    }

    @Override
    public void cancelB(int a, int b) {
        System.out.println("cancelB");
        jdbcTemplate2.execute("INSERT INTO `test_transaction`(`content`) VALUES ('cancelB');");
    }

    @CustomTransaction(confirmMethod = "confirmB", cancelMethod = "cancelB")
    @Override
    public void tryErrorB(int a, int b) {
        throw  new RuntimeException("模拟触发异常");
    }

    @CustomTransaction(confirmMethod = "confirmErrorB", cancelMethod = "cancelB")
    @Override
    public void trySuccessConfirmErrorB(int a, int b) {
        jdbcTemplate2.execute("INSERT INTO `test_transaction`(`content`) VALUES ('trySuccessConfirmErrorB');");
    }

    @Override
    public void confirmErrorB(int a, int b) {
        System.out.println("confirmErrorB");
        throw new RuntimeException("模拟触发异常");
    }
}
